﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Data
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Imports System.IO
Imports System.Net
Imports System.Text

Imports com.google.api

Imports Yahoo.API
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearch.Click
        If Page.IsValid Then
            Dim _Points As New BusinessLogic.Points
            _Points.Activity = 0
            _Points.IPAddress = Request.UserHostAddress
            _Points.Comments = txtKeyword.Text
            If Session("Username") IsNot Nothing Then
                _Points.Username = Session("Username")
            Else
                _Points.Username = ""
            End If
            _Points.Save()

            Session("YahooPage") = 1
            googleImageResult.Visible = True
            GetYahooImageSearchWebServiceResults()
            imgBlank.Height = 30
            imgFrame.Visible = False
            pnlHyperlinkBottom.Visible = True

        End If
    End Sub

    Private Sub GetYahooImageSearchWebServiceResults()
        pnlImage.Visible = True
        pnlImage1.Visible = False
        Dim srch As String = ""
        srch = txtKeyword.Text

        Dim yahoo As New YahooSearchService

        Dim _SearchResults As New ArrayList()
        Dim Count As Integer = 10
        Dim Offset As Integer = (Integer.Parse(Session("YahooPage").ToString()) - 1) * 10 + 1

        Dim resultSet As Yahoo.API.ImageSearchResponse.ResultSet
        Dim YahooApiKey As String = ConfigurationManager.AppSettings("YahooAPIKey").ToString
        Dim safeSearch As Boolean
        If Session("SafeSearch") Is Nothing Then
            safeSearch = True
        Else
            safeSearch = False
        End If
        resultSet = yahoo.ImageSearch(YahooApiKey, txtKeyword.Text, "all", Count, Offset, "any", safeSearch)
        For Each result As Yahoo.API.ImageSearchResponse.ResultType In resultSet.Result
            Dim _TempSearchResult As New SearchResult()
            _TempSearchResult.Image = result.Thumbnail.Url
            _TempSearchResult.Url = result.Url
            _TempSearchResult.Title = result.Title
            _TempSearchResult.FileFormat = result.FileFormat
            _TempSearchResult.FileSize = result.FileSize
            If result.Summary.Length > 20 Then
                _TempSearchResult.Summary = result.Summary.Substring(0, 17) & "..."
            Else
                _TempSearchResult.Summary = result.Summary
            End If
            _TempSearchResult.Summary = _TempSearchResult.Summary.ToLower()
            _TempSearchResult.Summary = _TempSearchResult.Summary.Replace(txtKeyword.Text.ToLower(), String.Format("<b>{0}</b>", txtKeyword.Text))
            Dim _temp = result.RefererUrl.Split("/")

            _TempSearchResult.RefererUrl = _temp(2)
            _TempSearchResult.ClickUrl = result.ClickUrl
            _TempSearchResult.Height = result.Height
            _TempSearchResult.Width = result.Width
            _SearchResults.Add(_TempSearchResult)
        Next
        litYahooSearchCount.Text = String.Format("Page {0} of {1}", Session("YahooPage"), CInt(resultSet.totalResultsAvailable / 10))
        If Integer.Parse(Session("YahooPage").ToString()) = 1 Then
            lnkYahooPrev.Visible = False
        Else
            lnkYahooPrev.Visible = True
        End If
        If Integer.Parse(Session("YahooPage").ToString()) = CInt(resultSet.totalResultsAvailable / 10) Then
            lnkYahooNext.Visible = False
        Else
            lnkYahooNext.Visible = True
        End If

        yahooImageRes.DataSource = _SearchResults
        yahooImageRes.DataBind()
        pnlImage1.Style.Remove("bottom")
        pnlImage1.Style.Add("bottom", "5px")
    End Sub
    Protected Sub lnkYahooPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkYahooPrev.Click
        Session("YahooPage") = Integer.Parse(Session("YahooPage").ToString()) - 1
        GetYahooImageSearchWebServiceResults()
    End Sub

    Protected Sub lnkYahooNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkYahooNext.Click
        Session("YahooPage") = Integer.Parse(Session("YahooPage").ToString()) + 1
        GetYahooImageSearchWebServiceResults()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If (Session("SafeSearch") Is Nothing) Then
                lnksafeSearch.Text = "turn off"
                lblSearch.Text = "SafeSearch is on"
            Else
                lnksafeSearch.Text = "turn on"
                lblSearch.Text = "SafeSearch is off"
            End If

            If Session("SearchText") IsNot Nothing Then
                txtKeyword.Text = Session("SearchText").ToString()
                Session("YahooPage") = 1
                googleImageResult.Visible = True
                GetYahooImageSearchWebServiceResults()
                imgBlank.Height = 30
                imgFrame.Visible = False
                pnlHyperlinkBottom.Visible = True
            End If
        Else
            If txtKeyword.Text.Length > 0 Then
                Session("SearchText") = txtKeyword.Text
            Else
                Session("SearchText") = Nothing
            End If
        End If
    End Sub

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.IsValid = True
        If txtKeyword.Text.Length = 0 Then
            args.IsValid = False
        End If
    End Sub

    Public Class SearchResult
        Private _Image As String
        Property Image() As String
            Get
                Return _Image
            End Get
            Set(ByVal value As String)
                _Image = value
            End Set
        End Property
        Private _Url As String
        Property Url() As String
            Get
                Return _Url
            End Get
            Set(ByVal value As String)
                _Url = value
            End Set
        End Property

        Private _Title As String
        Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property

        Private _RefererUrl As String
        Property RefererUrl() As String
            Get
                Return _RefererUrl
            End Get
            Set(ByVal value As String)
                _RefererUrl = value
            End Set
        End Property

        Private _ClickUrl As String
        Property ClickUrl() As String
            Get
                Return _ClickUrl
            End Get
            Set(ByVal value As String)
                _ClickUrl = value
            End Set
        End Property

        Private _Height As String
        Property Height() As String
            Get
                Return _Height
            End Get
            Set(ByVal value As String)
                _Height = value
            End Set
        End Property

        Private _Width As String
        Property Width() As String
            Get
                Return _Width
            End Get
            Set(ByVal value As String)
                _Width = value
            End Set
        End Property

        Private _FileSize As String
        Property FileSize() As String
            Get
                Return _FileSize
            End Get
            Set(ByVal value As String)
                _FileSize = value
            End Set
        End Property

        Private _FileFormat As String
        Property FileFormat() As String
            Get
                Return _FileFormat
            End Get
            Set(ByVal value As String)
                _FileFormat = value
            End Set
        End Property

        Private _Summary As String
        Property Summary() As String
            Get
                Return _Summary
            End Get
            Set(ByVal value As String)
                _Summary = value
            End Set
        End Property
    End Class


    Protected Sub lnksafeSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnksafeSearch.Click
        If (Session("SafeSearch") IsNot Nothing) Then
            lnksafeSearch.Text = "turn off"
            lblSearch.Text = "SafeSearch is on"
            Session("SafeSearch") = Nothing
        Else
            lnksafeSearch.Text = "turn on"
            lblSearch.Text = "SafeSearch is off"
            Session("SafeSearch") = True
        End If
    End Sub
End Class